/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.util.messaging;

import java.util.HashMap;

import org.activequant.util.messaging.events.MessageEvent;
import org.activequant.util.pattern.producer.Producer;


/**
 * An IMessengerService implementation implementation. Holds the following associated variables:
 * <ul>
 * <li>messengers(HashMap&lt;Protocol,IMessenger&gt;)</li>
 * <li>messageEvent(MessageEvent)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 * 21.12.2006 Created (en)
 *
 *  @author Erik Nijkamp
 */
public class MessagingService implements IMessengerService {
	/**
	 * private HashMap&lt;Protocol,IMessenger&gt; messengers = new HashMap&lt;Protocol,IMessenger&gt;();
	 */
    private HashMap<Protocol, IMessenger> messengers = new HashMap<Protocol, IMessenger>();
    /**
     * private MessageEvent messageEvent = new MessageEvent();
     */
    private MessageEvent messageEvent = new MessageEvent();
    /**
     * returns the associated messageEvent(MessageEvent)
     */
	public MessageEvent getMessageEvent() {
		return messageEvent;
	}
	/**
	 * constructs a MessagingService(IMessengerService implementation) using the given configs(MessengerConfig...) to initialize its 
	 * associated messengers(HashMap&lt;Protocol,IMessenger&gt;)
	 * @param configs
	 * @throws Exception
	 */
    public MessagingService(MessengerConfig... configs) throws Exception {
       setConfiguration(configs);
    }
    /**
     * initializes the associated messengers(HashMap&lt;Protocol,IMessenger&gt;) by creating an IMessenger implementation for every config(MessengerConfig) in the given
     * configs(MessengerConfig...).<br/>
     * Uses the implementationClass(String), accountName(String) and accountPassword(String) of a config(MessengerConfig) to create the IMessenger implementation. 
     * @param configs
     * @throws Exception
     */
    public void setConfiguration(MessengerConfig... configs) throws Exception {
       Producer<IMessenger> producer = new Producer<IMessenger>();
       // For each backend
       for (MessengerConfig config : configs) {
          // Create implementation using reflection
          IMessenger impl = producer.create(config.implementationClass, config.accountName, config.accountPassword);
          // Register listener
          impl.getMessageEvent().forward(messageEvent);
          // Add it to container
          messengers.put(impl.getProtocol(), impl);
       }
    }
    /**
     * invokes <code>messenger.connect()</code> on each IMessenger value in the associated messengers(HashMap&lt;Protocol,IMessenger&gt;)
     */
    public void connect() throws Exception {
    	for(IMessenger messenger: messengers.values()) {
    		messenger.connect();
    	}
    }
    /**
     * invokes <code>messenger.disconnect()</code> on each IMessenger value in the associated messengers(HashMap&lt;Protocol,IMessenger&gt;)
     */
    public void disconnect() throws Exception {
    	for(IMessenger messenger: messengers.values()) {
    		messenger.disconnect();
    	}
    }
    /**
     * invokes <code>messenger.sendMessage(to, subject, message)</code> on each IMessenger value in the associated messengers(HashMap&lt;Protocol,IMessenger&gt;)
     * @param to
     * @param subject
     * @param message
     * @throws Exception
     */
    public void sendMessage(String to, String subject, String message) throws Exception {
    	assert(!messengers.isEmpty());
    	for(IMessenger messenger: messengers.values()) {
    		messenger.sendMessage(to, subject, message);
    	}
	}
    /**
     * invokes <code>messenger.sendMessage(to, message)</code> on the IMessenger value mapped under the given protocol(Protocol) in the associated messengers(HashMap&lt;Protocol,IMessenger&gt;)
     */
    public void sendMessage(String to, String message, Protocol protocol) throws Exception {
		assert (!messengers.isEmpty());
		messengers.get(protocol).sendMessage(to, message);
	}
    /**
     * invokes <code>messenger.sendMessage(to, message)</code> on the IMessenger value mapped under the given protocol(Protocol) in the associated messengers(HashMap&lt;Protocol,IMessenger&gt;)
     */
	public void sendMessage(String[] to, String message, Protocol protocol) throws Exception {
		assert (!messengers.isEmpty());
		messengers.get(protocol).sendMessage(to, message);
	}
	/**
	 * invokes <code>messenger.sendMessage(to, subject, message)</code> on the IMessenger value mapped under the given protocol(Protocol) in the associated messengers(HashMap&lt;Protocol,IMessenger&gt;)
	 */
    public void sendMessage(String to, String subject, String message, Protocol protocol) throws Exception {
    	assert(!messengers.isEmpty());
		messengers.get(protocol).sendMessage(to, subject, message);
	}
    /**
     * invokes <code>messenger.sendMessage(to, subject, message)</code> on the IMessenger value mapped under the given protocol(Protocol) in the associated messengers(HashMap&lt;Protocol,IMessenger&gt;)
     */
	public void sendMessage(String[] to, String subject, String message, Protocol protocol) throws Exception {
		assert(!messengers.isEmpty());
		messengers.get(protocol).sendMessage(to, subject, message);
	}
}
